package com.baomidou.springwind.test.IO;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

/**
 * Created by Administrator on 2017/2/28.
 */
public class LargeMappedFiles {
    static int length = 0x8FFFFFF;
    static long endTime, startTime;
    public static void main(String[] args) throws IOException {
        MappedByteBuffer accessFile = new RandomAccessFile
                ("F:\\code\\SpringWind\\SpringWind\\src\\main\\java\\com\\baomidou\\springwind\\test\\IO\\xxx.txt", "rw")
                .getChannel().map(FileChannel.MapMode.READ_WRITE, 0, length);
        startTime = System.currentTimeMillis();
        for (int i = 0; i < length; i++) {
            accessFile.put((byte) 'x');
        }
        endTime = System.currentTimeMillis();
        System.out.println(endTime-startTime);
        System.out.println("Finish Write");
        startTime = System.currentTimeMillis();
        for (int i = length / 2; i < length / 2 + 6; i++) {
            System.out.println((char) accessFile.get(i));
        }
        endTime = System.currentTimeMillis();
        System.out.println(endTime-startTime);

        FileChannel fileChannel = new FileOutputStream("F:\\code\\SpringWind\\SpringWind\\src\\main\\java\\com\\baomidou\\springwind\\test\\IO\\1xxx.txt").getChannel();
        startTime = System.currentTimeMillis();
        for (int i = 0; i < length; i++) {
            fileChannel.write(ByteBuffer.wrap("x".getBytes()));
        }
        endTime = System.currentTimeMillis();
        System.out.println(endTime-startTime);
    }
}
